﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OrganizedIN.BAL.Common;
using System.IO;

namespace OrganizedIN.UI.Photos
{
    public partial class delete_photos : System.Web.UI.Page
    {
        DataSet ds = new DataSet();

        string strUserName = "";
        string imagepath = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string id = Request.QueryString["id"];
                strUserName = Convert.ToString(Session["UserID"]);
                string param = Request.QueryString["param"];
                if (!IsPostBack)
                {
                    if (id!=null)
                    {
                        DataSet ds_path = new DataSet();
                        ds_path = Common.getPhotoDetail(strUserName,Convert.ToInt32(id));
                        if (ds_path.Tables[0].Rows.Count > 0)
                        {
                            ds = Common.deletePhoto(strUserName, Convert.ToInt32(id));
                            imagepath = Server.MapPath(ds_path.Tables[0].Rows[0][4].ToString());
                            File.Delete(imagepath);
                            Response.Redirect("myphotos.aspx");
                        }
                    }
                    else
                    {
                        ds = Common.getPhotoDetail(strUserName);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            dgVideo.DataSource = ds.Tables[0];
                            dgVideo.DataBind();
                        }
                        else
                        {
                            gridwrapper.InnerHtml = "<h2>Sorry! No records found</h2>";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void dgVideo_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(e.Item.Cells[0].Text);
                ds = Common.deletePhoto(strUserName, id);
                imagepath = Server.MapPath(e.Item.Cells[1].Text);
                File.Delete(imagepath);
                ds = Common.getPhotoDetail(strUserName);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dgVideo.DataSource = ds.Tables[0];
                    dgVideo.DataBind();
                }
                else
                {
                    gridwrapper.InnerHtml = "<h2>Sorry! No records found</h2>";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
